﻿@inject I18n I18n
@typeparam TItem where TItem : IDefaultItem<TItem>

@foreach (var groupItem in _group)
{
    @if (groupItem.Key != ComponentType.Unknown)
    {
        <MDivider Inset Class="mt-3 mb-1"></MDivider>

        <MSubheader class="text--primary font-weight-black text-uppercase typed-list-header">
            <MIcon>mdi-circle-small</MIcon>
            @I18n.T("component-type." + groupItem.Key)
        </MSubheader>
    }

    @foreach (var item in groupItem)
    {
        <DefaultListItem Item="@item"></DefaultListItem>
    }
}

@code {

    IEnumerable<IGrouping<ComponentType, TItem>> _group = [];

    [Parameter] public List<TItem> Items { get; set; } = [];

    protected override void OnInitialized()
    {
        base.OnInitialized();

        List<TItem> flatItems = [];

        foreach (var item in Items)
        {
            if (item.HasChildren())
            {
                flatItems.AddRange(item.Children);
            }
            else
            {
                flatItems.Add(item);
            }
        }

        _group = flatItems.OrderBy(u => u.Title).GroupBy(u => u.ComponentType).OrderBy(u => u.Key);
    }

}